package com.example.jpa.dao;

import com.example.jpa.entity.Asset;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.UUID;

@Repository
public class AssetDao {

    private final JdbcTemplate jdbcTemplate;  // 主库 JdbcTemplate

    @Autowired
    public AssetDao(@Qualifier("jpaJdbcTemplate") JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    /**
     * 查询所有资产
     */
    public List<Asset> findAll() {
        String sql = "SELECT * FROM asset";
        return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Asset.class));
    }

    /**
     * 根据 ID 查询资产
     */
    public Asset findById(UUID id) {
        String sql = "SELECT * FROM asset WHERE id = ?";
        return jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(Asset.class), id);
    }

    /**
     * 根据租户 ID 查询资产
     */
    public List<Asset> findByTenantId(UUID tenantId) {
        String sql = "SELECT * FROM asset WHERE tenant_id = ?";
        return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Asset.class), tenantId);
    }

    /**
     * 分页查询资产
     */
    public List<Asset> findByPage(int offset, int limit) {
        String sql = "SELECT * FROM asset LIMIT ? OFFSET ?";
        return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Asset.class), limit, offset);
    }
}
